<template>
  <div class="drop-down-box">
    <el-collapse v-model="activeTitle" accordion @change="handleChange">
      <el-collapse-item
        v-for="(v, sensorListL) in sensorLists"
        :key="sensorListL"
        :name="v.officeFace"
      >
        <template slot="title">
          <div class="list-title">
            <div class="list-title-left">
              <img src="@/assets/img/monitor/icon-15.png" alt="" />
              <span class="titleName"> {{ v.officeFace }}</span>
            </div>
            <div>
              <img class="imgTran" :src="require(`@/assets/img/monitor/icon-${activeTitle ===v.officeFace ? '31' : '16' }.png`)" alt="" />
            </div>
          </div>
        </template>
        <div class="sensorbox">
          <div
            :class="['sensorList',
            current === i.cameraIndexCode ? 'active-video' : '']"
            v-for="(i, sensorMore) in v.equipment"
            :key="sensorMore"
            @click="sendMessage(i)"
          >
            <p class="title">
              <img
                v-if="i.online === 1"
                :src="require(`@/assets/img/monitor/icon-3.png`)"
                alt=""
              /><img
                v-if="i.online === 0"
                :src="require(`@/assets/img/monitor/icon-14.png`)"
                alt=""
              />{{ i.cameraName }}
            </p>
            <p class="workingF">{{ i.regionName }}</p>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>
<script>
export default {
  data() {
    return {
    };
  },
  props: {
    sensorLists: {
      type: Array,
      default: () => []
    }
  },
  computed: {
    ePointMonitor() {
      return this.$store.state.ePointMonitor;
    },
    activeTitle: {
      get() {
        return this.$store.state.ePointMonitor.activeTitle;
      },
      set(v) {
        console.log(v);
      }
    },
    current() {
      return this.$store.state.ePointMonitor.current;
    }
  },
  created() {
    this.$store.dispatch('listeningEvents', { fn: this.setData, _this: this });
  },
  beforeDestroy() {
    this.$store.dispatch('closeListeningEvents', { fn: this.setData, _this: this });
  },
  methods: {
    // 视频列表展开
    handleChange(val) {
      this.$store.commit('setEPointMonitor', { name: 'activeTitle', value: val });
    },
    // 点击视频列表详情
    sendMessage(item) {
      this.$store.commit('setData', { name: 'videoInfo', value: {
        isVideo: false,
        name: '',
        status: '',
        videoLink: ''
      }});
      const { cameraIndexCode } = item;
      // this.queryCameraUrl(cameraIndexCode);

      // 设置 current 值
      if (this.current === cameraIndexCode) {
        this.$store.commit('setEPointMonitor', { name: 'current', value: '' });
      } else {
        this.$store.commit('setEPointMonitor', { name: 'current', value: cameraIndexCode });
      }

      this.$store.dispatch('queryCameraUrl', { videoInfo: item });
    },

    // 通过引擎获取数据
    setData(msg) {
      if (msg.data) {
        const { action, type, entityData } = msg.data;
        if (type === 'EPointMonitor') {
          if (action === 'OnHidePanel') {
            this.$store.commit('setData', { name: 'ePointMonitor', value: { current: '', activeTitle: '' }});

            this.$store.commit('setData', { name: 'videoInfo', value: {
              isVideo: false,
              name: '',
              status: '',
              videoLink: ''
            }});
          } else if (action === 'OnShowPanel') {
            console.log(entityData);
            const params = {
              'cameraIndexCode': entityData?.id,
              'cameraName': entityData?.monitorName,
              'status': entityData?.status
            };
            this.$store.commit('setEPointMonitor', { name: 'current', value: entityData?.id });
            this.getActiveTitle(entityData?.id);

            this.$store.dispatch('queryCameraUrl', { videoInfo: params });
          }
        }

        msg.resolve({ response: 'successs' }); // 必须执行 msg.resolve()
      }
    },

    // 通过视频id，找到name
    getActiveTitle(id) {
      for (let index = 0; index < this.sensorLists.length; index++) {
        const element = this.sensorLists[index];
        const itemIndex = element?.equipment.findIndex(item => item.cameraIndexCode === id);
        if (itemIndex !== -1) {
          this.handleChange(element?.officeFace);
          this.goActivePosition(itemIndex, index);
          return false;
        }
      }
    },

    // 滚动到指定位置
    goActivePosition(itemIndex, index) {
      const activePosition = document.querySelector('.drop-down-box');
      activePosition.scrollTop = (itemIndex * 56) + (index * 16);
      console.log((itemIndex * 56) + (index * 16));
    }
  }
};
</script>
<style scoped>
* >>> .el-collapse-item__arrow {
  display: none;
}
* >>> .el-collapse-item__header {
  height: 1rem;
  color: rgba(248, 248, 248, 100);
  font-size: 14px;
  background-color: #04162f;
  border: 0;
  margin: 0.625rem;
}
* >>> .el-collapse {
  border: 0;
}
* >>> .el-collapse-item__wrap {
  background-color: #04162f;
  border: 0;
}
* >>> .el-collapse-item__content {
  padding: 0;
}
</style>
<style lang="scss"  scoped>
.drop-down-box {
  img {
    width: 0.875rem;
    height: 0.875rem;
  }
  .list-title{
    width: 19.25rem;
    height: 1rem;
    display: inline-flex;
    align-items: center;
    justify-content: space-between;
    .list-title-left{
      width: 90%;
      display: inline-flex;
      img {
        width: 0.875rem;
        height: 0.875rem;
      }
      .titleName {
        margin: 0 1.625rem 0 0.625rem;
        height: 1rem;
        display: flex;
        align-items: center;
      }
    }
  }
  .sensorbox {
    .sensorList:last-child {
      margin-bottom: 0;
    }
    .sensorList {
      height: 3.5rem;
      border-radius: 4px;
      background-color: rgba(255, 255, 255,  0.1);
      overflow: hidden;
      padding-left: 0.875rem;
      overflow: hidden;
      margin-bottom: 0.625rem;
      img {
        width: 0.875rem;
        height: 0.875rem;
        margin-right: 0.625rem;
      }
      .title {
        color: rgba(248, 248, 248, 100);
        font-size: 14px;
        display: flex;
        align-items: center;
        margin: 0.4rem 0 0.2rem 0;
      }
      .workingF {
        color: rgba(173, 173, 173, 100);
        font-size: 12px;
      }
    }
  }
  .active-video{
    background-color:rgba(255, 255, 255, .2) !important;
  }
}
</style>

